package com.haople.sso.jdbc.dao.impl;

import com.haople.sso.core.vo.User;
import com.haople.sso.jdbc.dao.BaseDao;
import com.haople.sso.jdbc.dao.UserDao;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户Dao
 */
@Repository("userDao")
public class UserDaoImpl extends BaseDao implements UserDao {
	private static final Logger logger=Logger.getLogger(UserDaoImpl.class);
	
	/**
	 * 根据用户名查询用户
	 */
    public User find(String userName) {
    	String sql="SELECT su.`user_id`,su.`user_name`,su.`password` FROM sso_user su WHERE su.user_name=?";
        try {
			return getJdbcTemplate().queryForObject(sql, new RowMapper<User>() {
			    public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			        User user=new User();
			        user.setPassword(rs.getString("password"));
			        user.setUserId(rs.getString("user_id"));
			        user.setUserName(rs.getString("user_name"));
			        return user;
			    }
			},userName);
		} catch (DataAccessException e) {
			logger.debug("---没有查询到此用户---[ userName:"+userName+" ]------",e);
		}
        return null;
    }

    
    
    /**
     * 保存用户信息
     * @param userName
     * @param password
     * @return
     */
   public Integer save(String userName,String password) {
    	String sql="inster into sso_user(`user_name`,`password`) values (?,?)";
    	return getJdbcTemplate().update(sql,userName,password);
    }
    
    /**
     * 更新用户密码
     * @param userName
     * @param password
     * @return
     */
   public Integer updatePassword(String userName,String password) {
    	String sql="update sso_user u set u.password=? where u.user_name=?";
    	return getJdbcTemplate().update(sql, password,userName);
    }
}
